package book.BouncyCube;

import javax.microedition.khronos.egl.EGL10;                                 //1 
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;                                         //2 
import java.lang.Math;

import android.util.Log;

public class CubeRenderer implements GLSurfaceView.Renderer
{
    public CubeRenderer(boolean useTranslucentBackground)
    {
        mTranslucentBackground = useTranslucentBackground;
        mCube = new Cube();                                              //3
    }

    public void onDrawFrame(GL10 gl)                                         //4
    {
        mAngle+=.4;


        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);     //5

        gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f);                                  //1

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

//        gl.glTranslatef(0, 0, -5);
        gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f);

        mCube.draw(gl);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);


        mTransY += .075f;
    }

    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
/////////////////////////////////////////////////
        float aspectRatio2, zNear2, zFar2, fov2;

        aspectRatio2=(float)width/( float)height;
        zNear2 = 0.1f;
        zFar2 = 1000f;
        fov2 = 3.1415f / 4.0f;  //Pi/4

        //把相片右边沿对准在x=2, 相片离自己为4
        float z = 4;  //坐标为-4
        float x = 2;
        fov2 = (float) Math.atan( x/z );


        float left,right,bottom,top;
        right = zNear2 * (float)Math.tan((double)fov2);
        top = right / aspectRatio2;
        left = -right;
        bottom = -top;

        gl.glViewport(0, 0, width, height);
        gl.glEnable(GL10.GL_NORMALIZE);
        gl.glMatrixMode(GL10.GL_PROJECTION);

        gl.glFrustumf(left, right, bottom, top, zNear2, zFar2);

        ///begin 设置相机位置
        gl.glTranslatef(0, 0, -4);
        gl.glRotatef(45, 1.0f, 0.0f, 0.0f);
        gl.glRotatef(-45, 0.0f, 1.0f, 0.0f);
        ///

        gl.glMatrixMode(GL10.GL_MODELVIEW);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config)          //15
    {
        gl.glDisable(GL10.GL_DITHER);                                        //16

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,                       //17
                GL10.GL_FASTEST);

        if (mTranslucentBackground)                                     //18
        {
            gl.glClearColor(0,0,0,0);
        }
        else
        {
            gl.glClearColor(1,1,1,1);
        }
        gl.glEnable(GL10.GL_CULL_FACE);                                     //19
        gl.glShadeModel(GL10.GL_SMOOTH);                                    //20
        gl.glEnable(GL10.GL_DEPTH_TEST);                                    //21
    }
    private boolean mTranslucentBackground;
    private Cube mCube;
    private float mTransY;
    private float mAngle;
}




